/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultarArticulo.java
 *
 * Created on 11/04/2011, 10:06:18 PM
 */

package Vistas;

import Beans.Articulo;
import Controladores.Marticulos;
import Imagenes.CtrlImagen;
import Vistas.Menu.Menu;
import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Usuario
 */
public class ConsultarArticulo extends javax.swing.JFrame {

    /** Creates new form ConsultarArticulo */
    private DefaultTableModel modeloTabla = new DefaultTableModel();
    Marticulos Ma = new Marticulos();
    Articulo a = new Articulo();
    int fila;
    private com.toedter.calendar.JDateChooser jDateChooser1;

    public ConsultarArticulo(){
        initComponents();
    }

    public ConsultarArticulo(Marticulos ma){

        this.Ma = ma;
        modeloTabla.addColumn("CODIGO");
        modeloTabla.addColumn("NOMBRE");
        modeloTabla.addColumn("  FECHA-VENCIMIENTO  ");
        modeloTabla.addColumn("    FEHCA-COMPRA   ");
        modeloTabla.addColumn("   FECHA-FABRICACION   ");
        modeloTabla.addColumn("CANTIDAD");
        modeloTabla.addColumn("PRECIO");

        for(int x=0;x<Ma.getListaarticulos().size();x++){
            Vector<String> vA = new Vector<String>();
             a = Ma.getListaarticulos().get(x);
             vA.add(""+a.getCodigo());
             vA.add(a.getNombre());
             vA.add((Ma.formateador.format(a.getFecha_vencimiento())));
             vA.add((Ma.formateador.format(a.getFecha_compra())));
             vA.add((Ma.formateador.format(a.getFecha_fabricacion())));
             vA.add(a.getCantidad()+"");
             vA.add(a.getPrecio()+"");
             modeloTabla.addRow(vA);

        }

        

        initComponents();
        CtrlImagen CI = new CtrlImagen("/Imagenes/day.jpg");
        this.jPanel1.add(CI,BorderLayout.CENTER);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jTable1.setModel(modeloTabla);
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("CONSULTAR ARTICULOS");

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/save.png"))); // NOI18N
        jButton3.setText("REGISTRAR");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/action_refresh.gif"))); // NOI18N
        jButton5.setText("Actualizar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        ListSelectionModel lsm =  jTable1.getSelectionModel();
        lsm.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                try{
                    if(e.getValueIsAdjusting())return;
                    ListSelectionModel ls=(ListSelectionModel )e.getSource();
                    if(ls.isSelectionEmpty()){
                        fila=-1;
                        jButton2.setEnabled(false);
                    }else{
                        fila=ls.getMinSelectionIndex();
                        jButton2.setEnabled(true);
                    }
                }catch(Exception z){
                }
            }
        });
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/delete.gif"))); // NOI18N
        jButton2.setText("ELIMINAR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        ListSelectionModel lm =  jTable1.getSelectionModel();
        lm.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                try{
                    if(e.getValueIsAdjusting())return;
                    ListSelectionModel l=(ListSelectionModel )e.getSource();
                    if(l.isSelectionEmpty()){
                        fila=-1;
                        jButton6.setEnabled(false);
                    }else{
                        fila=l.getMinSelectionIndex();
                        jButton6.setEnabled(true);
                    }
                }catch(Exception z){
                }
            }
        });
        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/icon_alert.gif"))); // NOI18N
        jButton6.setText("ESTADO");
        jButton6.setEnabled(false);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/action_back.gif"))); // NOI18N
        jButton4.setText("REGRESAR");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        ListSelectionModel lssm =  jTable1.getSelectionModel();
        lssm.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                try{
                    if(e.getValueIsAdjusting())return;
                    ListSelectionModel lss=(ListSelectionModel )e.getSource();
                    if(lss.isSelectionEmpty()){
                        fila=-1;
                        jButton1.setEnabled(false);
                    }else{
                        fila=lss.getMinSelectionIndex();
                        jButton1.setEnabled(true);
                    }
                }catch(Exception z){
                }
            }
        });
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/sql.gif"))); // NOI18N
        jButton1.setText("MODIFICAR");
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 996, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(jButton1)
                        .addGap(70, 70, 70)
                        .addComponent(jButton2)
                        .addGap(88, 88, 88)
                        .addComponent(jButton3)
                        .addGap(81, 81, 81)
                        .addComponent(jButton6)
                        .addGap(71, 71, 71)
                        .addComponent(jButton4)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(889, Short.MAX_VALUE)
                .addComponent(jButton5)
                .addGap(28, 28, 28))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(416, 416, 416)
                .addComponent(jLabel1)
                .addContainerGap(467, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(34, 34, 34)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 459, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton6)
                    .addComponent(jButton4))
                .addGap(23, 23, 23))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // Cargamos la vista registrar un articulo
        Reg_Articulos Ra = new Reg_Articulos(Ma);
        Ra.setLocationRelativeTo(null);
        Ra.setVisible(true);
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // Actualizar
        this.dispose();
        ConsultarArticulo Ca = new ConsultarArticulo(Ma);
        Ca.setLocationRelativeTo(null);
        Ca.setVisible(true);
}//GEN-LAST:event_jButton5ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // Eliminar un articulo
        int question = JOptionPane.showConfirmDialog(null,"¿Seguro que desea"
                + " eliminar este proveedor?","Informacion",JOptionPane.YES_NO_OPTION);
        if(question==0){
            int posicion=this.jTable1.getSelectedRow();
            Articulo Ar = new Articulo();
            Ar = Ma.getListaarticulos().get(posicion);
            modeloTabla.removeRow(posicion);
            Ma.Eliminar(Ar.getCodigo());
        }else{
            JOptionPane.showMessageDialog(null, "Peticion ca"
                    + "ncelada","Informacion",JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        //Estado y detalles del producto seleccionado

        int posicion = jTable1.getSelectedRow();
        Articulo AR = Ma.getListaarticulos().get(posicion);
        EstadoArticulos hoy = new EstadoArticulos(new Menu(),true,AR,posicion);
        hoy.setLocationRelativeTo(null);
        hoy.setVisible(true);
}//GEN-LAST:event_jButton6ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        //Cerramos esta ventana
        this.dispose();
}//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Modificar el producto seleccionado
        int Posicion = jTable1.getSelectedRow();
        Articulo ar=Ma.getListaarticulos().get(Posicion);
        ModArticulo MA = new ModArticulo(new Menu(),true,Ma,ar,Posicion);
        MA.MostrarDatos(ar);
        MA.setLocationRelativeTo(null);
        MA.setVisible(true);
}//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConsultarArticulo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
